package io.yuanqi.hgtcshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuanqi.hgtcshop.dao.MemberMapper;
import io.yuanqi.hgtcshop.entity.dos.Member;
import io.yuanqi.hgtcshop.entity.enums.ResultCode;
import io.yuanqi.hgtcshop.entity.security.Token;
import io.yuanqi.hgtcshop.exception.ServiceException;
import io.yuanqi.hgtcshop.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
/**
 * 会员接口业务层实现
 *
 * @author Chopper
 * @since 2021-03-29 14:10:16
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByMobile(String mobile) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        return this.baseMapper.selectOne(queryWrapper);
    }
    @Override
    public boolean findByMobile(String uuid, String mobile) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = this.baseMapper.selectOne(queryWrapper);
        if (member == null) {
            throw new ServiceException(ResultCode.USER_NOT_PHONE);
        }
        return true;
    }

    @Override
    @Transactional
    public Token register(String userName, String password, String mobilePhone) {
        //检测会员信息
        checkMember(userName, mobilePhone);
        //设置会员信息
        Member member = new Member(userName, password, mobilePhone);
        //注册成功后用户自动登录
        memberMapper.insert(member);
        return null;
    }

    @Override
    public Token register(JSONObject resultObj) {
        String openid = resultObj.getString("openid");
        String sex = resultObj.getString("sex");
        String nickname = resultObj.getString("nickname");
        String headimgurl = resultObj.getString("headimgurl");

        //检测会员信息
        checkMember(openid, openid);
        //设置会员信息
        Member member = new Member(openid, openid, openid);
        member.setFace(headimgurl);
        member.setCreateTime(new Date());
        member.setDeleteFlag(false);
        member.setUpdateBy(openid);
        member.setUpdateTime(new Date());
        member.setNickName(nickname);
//        member.setSex(Integer.valueOf(sex));
        member.setId(String.valueOf(System.currentTimeMillis()));
        //注册成功后用户自动登录
        memberMapper.insert(member);
        return null;
    }

    @Override
    public Member findByMemberId(String memberId) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", memberId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Member findByOpenId(String openId) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", openId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 检测会员
     *
     * @param userName    会员名称
     * @param mobilePhone 手机号
     */
    private void checkMember(String userName, String mobilePhone) {
        //判断手机号是否存在
        if (findMember(mobilePhone, userName) > 0) {
            throw new ServiceException(ResultCode.USER_EXIST);
        }
    }

    /**
     * 根据手机号获取会员
     *
     * @param mobilePhone 手机号
     * @return 会员
     */
    private Integer findMember(String mobilePhone, String userName) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobilePhone)
                .or().eq("username", userName);
        Integer i = this.baseMapper.selectCount(queryWrapper);
        return i;
    }
}